/**
 * 
 */
package sailing.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import org.jb2011.lnf.beautyeye.utils.NinePatchHelper;
import org.jdesktop.application.SingleFrameApplication;

import sailing.resource.Resource;
import sailing.ui.base.ITabPanel;
import cjlite.logger.Logger;

import com.android.ninepatch.NinePatch;
/**
 * @author kevin
 * 
 */
public class MainPanel extends AbstractJPanel {

	private static Logger logger = Logger.thisClass();
	//private static Color bgColor = new Color(240, 240, 240,0);
	private static Color bgColor = new Color(205, 50, 50, 0);
	
	private static NinePatch bgnp;
	/**
	 * 
	 */
	private static final long serialVersionUID = -8245231719198240199L;
	//
	JTabbedPane tabbedpane;

	public MainPanel(SingleFrameApplication application) {
		super(application);
	}

	@Override
	protected void initialPanel() {
		this.setLayout(new BorderLayout());
		this.setBackground(bgColor);
		tabbedpane = new JTabbedPane();
		//tabbedpane.setOpaque(false);
		tabbedpane.setBorder(new EmptyBorder(5, 5, 50, 5));
		tabbedpane.setBackground(bgColor);

		// 1.现金日记账
		ITabPanel cashAccount = this.getCashAccount();
		this.addTab(cashAccount);

		// 2.银行存款帐
		ITabPanel bankDepositsAccount = this.getBankDepositsAccount();
		this.addTab(bankDepositsAccount);

		// 3.银行对账单
		ITabPanel bankStatement = this.getBankStatement();
		this.addTab(bankStatement);

		// 4.其他应付账
		ITabPanel billsDetail = this.getBillsDetail();
		this.addTab(billsDetail);

		// 5.项目回款明细
		ITabPanel receivableDetail = this.getReceivableDetail();
		this.addTab(receivableDetail);

		// 6.借款项目明细
		ITabPanel loanAccountDetail = this.getLoanAccountDetail();
		this.addTab(loanAccountDetail);

		// 7.往来账目明细
		ITabPanel runningAccountDetail = this.getRunningAccountDetail();
		this.addTab(runningAccountDetail);

		// 8.系统设置
		ITabPanel systemSetting = this.getSystemSetting();
		this.addTab(systemSetting);

		this.add(tabbedpane, BorderLayout.CENTER);
	}

	/**
	 * Insert TabPanel into tabbedpane
	 * 
	 * @param tabPanel
	 */
	private void addTab(ITabPanel tabPanel) {
		if (tabPanel == null) {
			return;
		}

		if (tabPanel.getTabName() == null || tabPanel.getContentPanel() == null) {
			logger.error("tab Panel[{0}] is not legal ", tabPanel.getClass().getName());
			return;
		}
		tabbedpane.addTab(tabPanel.getTabName(), tabPanel.getTabIcon(), tabPanel.getContentPanel(),
				tabPanel.getTabTip());
		
	}

	private ITabPanel getCashAccount() {
		return new CashAccountPanel(this.getApplication());
	}

	private ITabPanel getBankDepositsAccount() {
		return new BankDepositsAccountPanel(this.getApplication());
	}

	private ITabPanel getBankStatement() {
		return new BankStatementPanel(this.getApplication());
	}

	private ITabPanel getBillsDetail() {
		return new BillsDetailPanel(this.getApplication());
	}

	private ITabPanel getReceivableDetail() {
		return new ReceivableDetailPanel(this.getApplication());
	}

	private ITabPanel getLoanAccountDetail() {
		return new LoanAccountDetailPanel(this.getApplication());
	}

	private ITabPanel getRunningAccountDetail() {
		return new RunningAccountDetailPanel(this.getApplication());
	}

	private ITabPanel getSystemSetting() {
		return new SystemSettingPanel(this.getApplication());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	protected void paintComponent(Graphics g) {
		String bg9Img="/ui/main/bg.9.png";
//		ImageIcon icon = Resource.getImageIcon("/ui/main/background2.jpg");
//
//		g.drawImage(icon.getImage(), 0, 0, (int) getApplication().getMainFrame().getSize().getWidth(), (int) getApplication().getMainFrame().getSize().getHeight(),
//				icon.getImageObserver());
//		//g.drawImage(icon.getImage(), 0, 0, icon.getImageObserver());

		if(bgnp==null){
			bgnp=NinePatchHelper.createNinePatch(Resource.getResource(bg9Img), false);
		}
		int width=(int) getApplication().getMainFrame().getSize().getWidth();
		int height=(int) getApplication().getMainFrame().getSize().getHeight();
		bgnp.draw((Graphics2D)g, 0, 0, width-10,height-31);
		super.paintComponent(g);
	}



}
